#include <exception>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/timeb.h>
#include <readline/readline.h>
#include <readline/history.h>

using namespace std;

#include "SynthUnits.h"

void testUnits()
{
   sample_t buf[25] = {0};

   Noise n;

   Gain g1 = Gain().setGain(0.2);
   connect(n, g1);

   SinOsc s = SinOsc().setFreq(440).setPhase(0);
   Gain g = Gain().setGain(1);
   connect(g1, g);
   connect(s, g);

   g.ar(0, buf, 25, Options().setIsDebugBuffer(true).setAddition(true));

   //g.ar(0, buf, 5, Options().setIsDebugBuffer(true));
}

void testCLI()
{
   s7_scheme *s7 = Script::getInstance().getS7();
   char *str = NULL, *line = NULL;

   rl_initialize();
   using_history();

   while ((line = readline("s7> ")))
   {
      str = line;
      while (isspace(*str)) str ++;
      if (*str == 0) continue;

      s7_pointer result = s7_eval_c_string(s7, str);
      s7_write(s7, result, s7_current_output_port(s7));
      s7_newline(s7, s7_current_output_port(s7));
      add_history(str);
   }
}

int main(int argc, char **argv)
{
   /* Initialize random number generator */
   struct timeb t;
   ftime(&t);
   srand(t.millitm);

   testCLI();
   //testUnits();

   return 0;
}
